package util;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import socket.handler.ClientSocketHandler;
import socket.handler.SocketHandler;
import socket.receiver.ClientReceiver;

import android.content.Context;

public class ConnectUtil {
	private static SocketHandler handler;
	private static final String address="192.168.1.22";
	private static final int time_out=3000;
	public static SocketHandler getHandler(Context context) throws UnknownHostException, IOException{
		if(handler==null){
			Socket socket = new Socket(address, 5050);		
			socket.setSoTimeout(time_out);
			ObjectOutputStream out=new ObjectOutputStream(socket.getOutputStream());
			out.flush();
			ObjectInputStream in=new ObjectInputStream(socket.getInputStream());
			handler=new ClientSocketHandler(socket, in, out,context);	
			new ClientReceiver(handler).start();
		}
		else{
			handler.setContext(context);
		}
		return handler;
	}
	public static void disconnect(){
		handler=null;
	}
}
